package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Option;

@Repository(value = "optionDao")
@Transactional(readOnly = false)
public class OptionDaoImpl implements OptionDao {

	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	@SuppressWarnings("unchecked")
	public List<Option> getOptionList() {
		return em.createNamedQuery("Option.findAll").getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Option> getOptionList(short menuId) {
		Query  q =em.createNamedQuery("Option.findByMenuId");
		q.setParameter("menuId",menuId);		
    	return q.getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Option> getOptionList(short menuId, short submenuId) {
		Query  q =em.createNamedQuery("Option.findByMenuIdBySubmenuId");
		q.setParameter("menuId",menuId);		
		q.setParameter("submenuId",submenuId);		
    	return q.getResultList();
	}
	public Option findByMenuIdBySubmenuIdByOptionId(short menuId, short submenuId, short optionId) {
    	Option res=null;
    	Query  q = em.createNamedQuery("Option.findByMenuIdBySubmenuIdByOptionId");
    	q.setParameter("menuId",menuId);
    	q.setParameter("submenuId",submenuId);
    	q.setParameter("optionId",optionId);
    	if(q.getResultList().size()==1)
    		res=(Option)q.getSingleResult();
    	return res;
	}
	public void add(Option option) {
		em.persist(option);
	}
}
